<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%!
public void check(String [] arr)
{
	for(int i=0;i<arr.length;i++)
	{
		while(arr[i] != null && arr[i].indexOf("\n") != -1)
		{
			arr[i] = arr[i].substring(0,arr[i].indexOf("\n")-1) + "<br/>" + arr[i].substring(arr[i].indexOf("\n")+1,arr[i].length());
		}
	}
}
%>
<%
String usernameS = (String) session.getAttribute("session_name");
String typeS = (String) session.getAttribute("type");
String idS = (String) session.getAttribute("id");
if( usernameS == null || !typeS.equals("root"))
{
	response.sendRedirect("error.jsp");
}
else
{	
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
String [] id = null;
String [] events = null;
String [] eresult = null;
String [] edate = null;
String [] etime = null;
String [] etype = null;

SimpleDateFormat day = new SimpleDateFormat("MM/dd/yyyy");
String dateE = day.format(new Date());

int size = 0;
try
{
	Class.forName(driver).newInstance();
	
	Connection connection = DriverManager.getConnection(url, username, password);
	
	Statement statement = connection.createStatement();
	Statement statement1 = connection.createStatement();
	Statement statementS1 = connection.createStatement();
	Statement statementS2 = connection.createStatement();
	
	String query = "select count(*) from events";
	String query1 = "select * from events";
	String queryS1;
	String queryS2;
	
	ResultSet rs = statement.executeQuery(query);
	if(rs.next())
	{
		size = rs.getInt(1);
	}

	id = new String[size];
	events = new String[size];
	eresult = new String[size];
	edate = new String[size];
	etime = new String[size];
	etype = new String[size];
	
	ResultSet rs1 = statement1.executeQuery(query1);		
	for(int i=0 ;rs1.next();i++)
	{
		id[i] = rs1.getString("no");
		events[i] = rs1.getString("event");
		eresult[i] = rs1.getString("result");
		edate[i] = rs1.getString("event_date");
		etime[i] = rs1.getString("event_time");
		etype[i] = rs1.getString("type");
	}
	check(events);
	check(eresult);
	check(edate);
	check(etime);
	check(etype);
	
	connection.close();
		
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
		out.println("<br>SQL Exception: " + sqle);
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
}
out.println("<script type='text/javascript'>");
out.println("function init() {");
for(int i = 0;i<size;i++) {
	out.println("descriptions["+i+"] = new Desc();");
	out.println("descriptions["+i+"].id = '"+ (id[i] != null ? id[i] : "") +"';");
	out.println("descriptions["+i+"].events = '"+ (events[i] != null ? events[i] : "") +"';");
	out.println("descriptions["+i+"].eresult = '"+(eresult[i] != null ? eresult[i] : "")+"';");
	out.println("descriptions["+i+"].edate = '"+(edate[i] != null ? edate[i] : "")+"';");
	out.println("descriptions["+i+"].etime = '"+(etime[i] != null ? etime[i] : "")+"';");
	out.println("descriptions["+i+"].etype = '"+(etype[i] != null ? etype[i] : "")+"';");
}
out.println("}");
out.println("var dateE ='"+dateE+"';");
out.println("</script>");

%>

<%
}
%>
<html>
<head>

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<link rel="STYLESHEET" href="styles/project.css" type="text/css">
<script type="text/javascript" language="javascript" src="js/project.js"></script>
<script type="text/javascript">

function events(x)
{
	var div = document.getElementById("event");
	var selct = document.getElementById("eventType");
	var out = "";
	var styl = "";
	var size = 0;
	out = "<table style='width:100%' border=2><tr><th>Event No.</th><th colspan='4'>Event Title</th>";
	out += "<th colspan='4'>Event Result</th><th>Event Date</th><th>Event Time</th><th>Event Type</th></tr>";
	if(x == 1 || selct.value=="today" )
	{
		
		for(var i = 0; i < descriptions.length; i++)
		{
			if(descriptions[i].etype == "login")
				styl="login";
			else
				styl="project";
				
			if(descriptions[i].edate == dateE)
			{
				out += "<tr class="+styl+"><td>"+descriptions[i].id+"</td><td colspan=4>"+descriptions[i].events+"</td>";
				out += "<td colspan=4>"+descriptions[i].eresult+"</td><td>"+descriptions[i].edate+"</td>";
				out += "<td>"+descriptions[i].etime+"</td><td>"+descriptions[i].etype+"</td></tr>";
				size++;
			}
			
		}
	}
	else if(selct.value == "all")
	{
		for(var i = 0; i < descriptions.length; i++)
		{
			if(descriptions[i].etype == "login")
				styl="login";
			else
				styl="project";
				
			out += "<tr class="+styl+"><td>"+descriptions[i].id+"</td><td colspan=4>"+descriptions[i].events+"</td>";
			out += "<td colspan=4>"+descriptions[i].eresult+"</td><td>"+descriptions[i].edate+"</td>";
			out += "<td>"+descriptions[i].etime+"</td><td>"+descriptions[i].etype+"</td></tr>";
			size++;
		}
	}
	else
	{
		var type = selct.value;
		for(var i = 0; i < descriptions.length; i++)
		{
			if(descriptions[i].etype == type)
			{
				out += "<tr class="+type+"><td>"+descriptions[i].id+"</td><td colspan=4>"+descriptions[i].events+"</td>";
				out += "<td colspan=4>"+descriptions[i].eresult+"</td><td>"+descriptions[i].edate+"</td>";
				out += "<td>"+descriptions[i].etime+"</td><td>"+descriptions[i].etype+"</td></tr>";
				size++;
			}
		}
	}
	
	if( size == 0)
			out += "<tr><td colspan=12><center><h2>No result</h2></td></tr><center>";
	out += "</table>";
	div.innerHTML = out;

}

function startE()
{
	init();
	events(1)
}


	
</script>
</head>
<body onload="startE()">
<table style="width:100%">
	<tr><td><input type="button" value="&nbsp;&nbsp;Back&nbsp;&nbsp;" onclick="history.go(-1)"/></td>
	<td colspan=10><center><select style="width:150" id="eventType">
	<option value="all">All</option>
	<option value="today">Today</option>
	<option value="project">Projects Events</option>
	<option value="login">Login Events</option>
	</select>
	<input type="button" value="Search" onclick="events(0)"/></center>
	</td>
	

<br>
	
</tr>
</table>
<div id="event">

</div>
<center><input type="button" value="&nbsp;&nbsp;Back&nbsp;&nbsp;" onclick="history.go(-1)"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="print events" onclick="window.print()"/></center>
</body>
</html>